
package com.dg.acb.utils;

import java.util.Collection;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Base64;

public class ParcelableJson extends JSONObject implements Parcelable {

    public ParcelableJson() {
    }

    public ParcelableJson(String json) throws JSONException {
        super(json);
    }

    public ParcelableJson(JSONObject object) throws JSONException {
        super(object.toString());
    }

    public static final Creator<ParcelableJson> CREATOR = new Creator<ParcelableJson>() {

        @Override
        public ParcelableJson createFromParcel(Parcel parcel) {
            try {
                return new ParcelableJson(parcel.readString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return new ParcelableJson();
        }

        @Override
        public ParcelableJson[] newArray(int size) {
            return new ParcelableJson[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int arg1) {
        parcel.writeString(this.toString());
    }

    public int getInt(String name, int defaultVaule) {
        if (has(name)) {
            try {
                return super.getInt(name);
            } catch (JSONException ignored) {
            }
        }
        return defaultVaule;
    }

    public long getLong(String name, long defaultVaule) {
        if (has(name)) {
            try {
                return super.getLong(name);
            } catch (JSONException ignored) {
            }
        }
        return defaultVaule;
    }

    public String getString(String name) {
        if (has(name)) {
            try {
                return super.getString(name);
            } catch (JSONException ignored) {
            }
        }
        return null;
    }

    @Override
    public JSONArray getJSONArray(String name) {
        try {
            return super.getJSONArray(name);
        } catch (JSONException ignored) {
        }
        return null;
    }

    @Override
    public JSONObject getJSONObject(String name) {
        try {
            return super.getJSONObject(name);
        } catch (JSONException ignored) {
        }
        return null;
    }

    public void putData(String name, int vaule) {
        try {
            super.put(name, vaule);
        } catch (JSONException ignored) {
            ignored.printStackTrace();
        }
    }

    public void putData(String name, long vaule) {
        try {
            super.put(name, vaule);
        } catch (JSONException ignored) {
            ignored.printStackTrace();
        }
    }

    public void putData(String name, String vaule) {
        try {
            super.put(name, vaule);
        } catch (JSONException ignored) {
            ignored.printStackTrace();
        }
    }

    public void putData(String name, ParcelableJson vaule) {
        try {
            super.put(name, vaule);
        } catch (JSONException ignored) {
            ignored.printStackTrace();
        }
    }

    public void putCollection(String name,
            Collection<? extends ParcelableJson> value) {
        if (!value.isEmpty()) {
            try {
                put(name, new JSONArray(value));
            } catch (JSONException ignored) {
                ignored.printStackTrace();
            }
        }
    }

    public void putBinaryData(String key, byte[] binaryData) {
        String encodeData = Base64.encodeToString(binaryData,
                Base64.DEFAULT);
        putData(key, encodeData);
    }

    public byte[] getBinaryData(String key) {
        String bValue = getString(key);
        if (!TextUtils.isEmpty(bValue)) {
            return Base64.decode(bValue, 0);
        }
        return null;
    }
}
